<template>
  <div>
    <BasicModal
      width="80%"
      v-bind="$attrs"
      @register="registerModal"
      :title="getTitle"
      @ok="handleSubmit"
    >
      <BasicForm @register="registerForm">
        <template #localSearch="{ model, field }">
          <ApiSelect
            :api="GetcitylistApi"
            showSearch
            v-model:value="model[field]"
            optionFilterProp="label"
            resultField="list"
            labelField="name"
            valueField="id"
          />
        </template>
      </BasicForm>
    </BasicModal>
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { BasicForm, useForm, ApiSelect } from '/@/components/Form/index';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { AddApi, EditApi, GetcitylistApi } from '/@/api/advertising/index';
  import { FormSchema } from '/@/components/Table';
  import { genderTypes, ADTypes } from '/@/assets/dic';

  export const formSchema: FormSchema[] = [
    {
      label: 'id',
      field: 'id',
      component: 'Input',
      show: false,
    },
    {
      field: 'city',
      label: '所属城市:',
      component: 'Citydropdown',
      // required: true,
      colProps: { lg: 24, md: 24 },
    },
    {
      field: 'type',
      label: '广告类别:',
      component: 'Select',
      componentProps: {
        options: ADTypes,
      },
      // required: true,
      defaultValue: ADTypes[0].value,
    },
    {
      field: 'title',
      label: '标题:',
      component: 'Input',
      // required: true,
      slot: 'localSearch',
      colProps: { lg: 24, md: 24 },
    },
    {
      field: 'sort',
      label: '排序:',
      component: 'Input',
      // required: true,
      colProps: { lg: 24, md: 24 },
    },
    // {
    //   field: 'status',
    //   label: '状态:',
    //   component: 'RadioButtonGroup',
    //   defaultValue: '0',
    //   componentProps: {
    //     options: [
    //       { label: '启用', value: '0' },
    //       { label: '禁用', value: '1' },
    //     ],
    //   },
    // },
  ];

  export default defineComponent({
    name: 'SalesUserModal',
    components: { BasicModal, BasicForm, ApiSelect },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const isUpdate = ref(true);

      const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
        labelWidth: 160,
        schemas: formSchema,
        showActionButtonGroup: false,
        baseColProps: { lg: 12, md: 24 },
      });

      const [registerModal, { closeModal }] = useModalInner(async (data) => {
        resetFields();
        isUpdate.value = !!data?.isUpdate;

        if (unref(isUpdate)) {
          setFieldsValue({
            ...data.record,
          });
        }
      });

      const getTitle = computed(() => (!unref(isUpdate) ? '新增销售人员' : '编辑销售人员'));

      async function handleSubmit() {
        const values = await validate();
        // values.parentId = 0 | values.parentId;
        if (values.id > 0) {
          await EditApi(values);
        } else {
          await AddApi(values);
        }
        console.log(values);
        closeModal();
        emit('success');
      }

      return { registerModal, registerForm, getTitle, handleSubmit, GetcitylistApi };
    },
  });
</script>
